function initCalendar() {
    const header = document.getElementById('calendar-header');
    const grid = document.getElementById('calendar-grid');
    if (!header || !grid) return;

    const today = new Date();
    const year = today.getFullYear();
    const month = today.getMonth();
    const day = today.getDate();

    header.textContent = `${year}年${month + 1}月`;
    
    // 获取节假日数据并生成日历
    getHolidays(year, month).then(holidays => {
        generateCalendarGrid(grid, year, month, day, holidays);
    });
}

// 生成日历网格（增加节假日参数）
function generateCalendarGrid(grid, year, month, todayDay, holidays = {}) {
    grid.innerHTML = '';

    // 添加星期标题
    const weekdays = ['一', '二', '三', '四', '五', '六', '日'];
    weekdays.forEach(day => {
        const weekdayCell = document.createElement('div');
        weekdayCell.className = 'calendar-weekday';
        weekdayCell.textContent = day;
        grid.appendChild(weekdayCell);
    });

    // 计算当月数据
    const firstDayOfMonth = new Date(year, month, 1).getDay() || 7;
    const totalDaysOfMonth = new Date(year, month + 1, 0).getDate();

    // 添加空白格子
    for (let i = 1; i < firstDayOfMonth; i++) {
        const emptyCell = document.createElement('div');
        emptyCell.className = 'calendar-empty';
        grid.appendChild(emptyCell);
    }

    // 添加日期格子（包含节假日判断）
    for (let day = 1; day <= totalDaysOfMonth; day++) {
        const dayCell = document.createElement('div');
        dayCell.className = 'calendar-day';
        dayCell.textContent = day;

        // 标记今天
        if (day === todayDay) {
            dayCell.classList.add('calendar-today');
        }

        // 标记节假日
        const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
        if (holidays[dateStr]) {
            dayCell.classList.add('calendar-holiday');
            dayCell.title = holidays[dateStr]; // 鼠标悬停显示节假日名称
        }

        grid.appendChild(dayCell);
    }
}

// 获取节假日数据（模拟数据，可替换为真实API）
async function getHolidays(year, month) {
    // 模拟节假日数据（示例）
    const mockHolidays = {
        '2024-01-01': '元旦',
        '2024-02-10': '春节',
        '2024-04-05': '清明节',
        '2024-05-01': '劳动节',
        '2024-06-10': '端午节',
        '2024-10-01': '国庆节'
    };
    return mockHolidays;
}

function cleanupCalendar() {
    const grid = document.getElementById('calendar-grid');
    if (grid) grid.innerHTML = '';
}